//*****************************************************************************
// FILE:            WinMTRNet.h
//
//    Copyleft (C) 2000-2002, Vasile Laurentiu Stanimir ( stanimir@cr.nivis.com )
//
// DESCRIPTION:
//   
//
// NOTES:
//    
//
//*****************************************************************************

#ifndef WINMTRNET_H_
#define WINMTRNET_H_


#include "WinMTRDialog.h"

struct ICMPHeader {
  __int8 type;
  __int8 code;
  __int16 checksum;
  __int16 id;
  __int16 sequence;
};

struct s_nethost {
  __int32 addr;
  int xmit;
  int returned;
  unsigned long total;
  int last;
  int best;
  int worst;
  int transit;
  int saved[SAVED_PINGS];
  char name[255];
};

struct s_sequence {
    int index;
    int transit;
    int saved_seq;
    struct timeval time;
};

//*****************************************************************************
// CLASS:  WinMTRNet
//
//
//*****************************************************************************

class WinMTRNet {

public:

	int		Preopen(WinMTRDialog *wp);
	int		Open(int address, int laddress);
	void	Reset();
	void	Close();

	int		GetWaitFd();
	void	ProcessReturn();
	int		GetLast(int at);
	int		GetMax();
	int		GetAddr(int at);
	int		GetName(int at, char *n);
	void	SetName(int at, char *n);
	int		GetPercent(int at);
	int		GetBest(int at);
	int		GetWorst(int at);
	int		GetAvg(int at);
	int		SendBatch();
	void	EndTransit();

	int		CalcDeltatime (float waittime);

	int		GetReturned(int at);
	int		GetXmit(int at);
	int		GetTransit(int at);

	int*	GetSavedPings(int at);
	void	SaveXmit(int at);
	void	SaveReturn(int at, int seq, int ms);
	int		GetDuplicate(int at, int seq);

	int		GetNumHosts()		{ return numhosts; };

	BOOL	FullLoop()			{ return full_loop; };

	float WaitTime;

	WinMTRDialog *wmtrdlg;

private:

	unsigned short Checksum(unsigned short  *lpBuf, int nLen) ;
	int GetNewSequence(int index);
	void SendQuery(int index);
	void ProcessPing(int seq, __int32 addr, struct timeval now);

	int packetsize;
	
	struct s_nethost host[MaxHost];
	struct s_sequence sequence[MaxSequence];
	struct timeval reset; 

	int timestamp;
	int sendsock;
	int recvsock;
	struct sockaddr_in remoteaddress;
	struct sockaddr_in localaddress;
	int batch_at; 
	int numhosts;

	BOOL full_loop;
};

#endif	// ifndef WINMTRNET_H_
